package com.ruoyi.web.controller.repair;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.repair.domain.TFaultReport;
import com.ruoyi.repair.service.ITFaultReportService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 故障报修Controller
 * 
 * @author ruoyi
 * @date 2025-09-29
 */
@RestController
@RequestMapping("/system/fault-report")
public class TFaultReportController extends BaseController
{
    @Autowired
    private ITFaultReportService tFaultReportService;

    /**
     * 查询故障报修列表
     */
    @PreAuthorize("@ss.hasPermi('system:fault-report:list')")
    @GetMapping("/list")
    public TableDataInfo list(TFaultReport tFaultReport)
    {
        startPage();
        List<TFaultReport> list = tFaultReportService.selectTFaultReportList(tFaultReport);
        return getDataTable(list);
    }

    /**
     * 导出故障报修列表
     */
    @PreAuthorize("@ss.hasPermi('system:fault-report:export')")
    @Log(title = "故障报修", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TFaultReport tFaultReport)
    {
        List<TFaultReport> list = tFaultReportService.selectTFaultReportList(tFaultReport);
        ExcelUtil<TFaultReport> util = new ExcelUtil<TFaultReport>(TFaultReport.class);
        util.exportExcel(response, list, "故障报修数据");
    }

    /**
     * 获取故障报修详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:fault-report:query')")
    @GetMapping(value = "/{reportNo}")
    public AjaxResult getInfo(@PathVariable("reportNo") String reportNo)
    {
        return success(tFaultReportService.selectTFaultReportByReportNo(reportNo));
    }

    /**
     * 新增故障报修
     */
    @PreAuthorize("@ss.hasPermi('system:fault-report:add')")
    @Log(title = "故障报修", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TFaultReport tFaultReport)
    {
        return toAjax(tFaultReportService.insertTFaultReport(tFaultReport));
    }

    /**
     * 修改故障报修
     */
    @PreAuthorize("@ss.hasPermi('system:fault-report:edit')")
    @Log(title = "故障报修", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TFaultReport tFaultReport)
    {
        return toAjax(tFaultReportService.updateTFaultReport(tFaultReport));
    }

    /**
     * 删除故障报修
     */
    @PreAuthorize("@ss.hasPermi('system:fault-report:remove')")
    @Log(title = "故障报修", businessType = BusinessType.DELETE)
    @DeleteMapping("/{reportNos}")
    public AjaxResult remove(@PathVariable String[] reportNos)
    {
        return toAjax(tFaultReportService.deleteTFaultReportByReportNos(reportNos));
    }

    /**
     * 提交故障报修（生成维修单）
     */
    @PreAuthorize("@ss.hasPermi('system:fault-report:submit')")
    @Log(title = "故障报修", businessType = BusinessType.UPDATE)
    @PostMapping("/submit/{reportNo}")
    public AjaxResult submit(@PathVariable("reportNo") String reportNo)
    {
        return toAjax(tFaultReportService.submitFaultReport(reportNo));
    }

    /**
     * 撤回故障报修
     */
    @PreAuthorize("@ss.hasPermi('system:fault-report:withdraw')")
    @Log(title = "故障报修", businessType = BusinessType.UPDATE)
    @PostMapping("/withdraw/{reportNo}")
    public AjaxResult withdraw(@PathVariable("reportNo") String reportNo)
    {
        return toAjax(tFaultReportService.withdrawFaultReport(reportNo));
    }

    /**
     * 承认故障报修
     */
    @PreAuthorize("@ss.hasPermi('system:fault-report:acknowledge')")
    @Log(title = "故障报修", businessType = BusinessType.UPDATE)
    @PostMapping("/acknowledge/{reportNo}")
    public AjaxResult acknowledge(@PathVariable("reportNo") String reportNo)
    {
        return toAjax(tFaultReportService.acknowledgeFaultReport(reportNo));
    }

    /**
     * 拒绝承认故障报修
     */
    @PreAuthorize("@ss.hasPermi('system:fault-report:reject')")
    @Log(title = "故障报修", businessType = BusinessType.UPDATE)
    @PostMapping("/reject/{reportNo}")
    public AjaxResult reject(@PathVariable("reportNo") String reportNo)
    {
        return toAjax(tFaultReportService.rejectFaultReport(reportNo));
    }

    /**
     * 获取故障报修状态履历
     */
    @PreAuthorize("@ss.hasPermi('system:fault-report:query')")
    @GetMapping(value = "/{reportNo}/history")
    public AjaxResult getHistory(@PathVariable("reportNo") String reportNo)
    {
        return success(tFaultReportService.getStatusHistory(reportNo));
    }

    /**
     * 检查设备是否有未完成的报修
     */
    @PreAuthorize("@ss.hasPermi('system:fault-report:query')")
    @GetMapping(value = "/check-unfinished/{assetNo}")
    public AjaxResult checkUnfinishedReport(@PathVariable("assetNo") String assetNo)
    {
        boolean hasUnfinished = tFaultReportService.hasUnfinishedReport(assetNo);
        return success(hasUnfinished);
    }
}
